//
//  FourDUanziTableViewController.m
//  开心一刻
//
//  Created by lanou on 16/4/7.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "FourDUanziTableViewController.h"
#import "Requestmanager.h"
#import "PictroctModel.h"
#import "FourTableViewCell.h"
#import "FourXiangqingViewController.h"
@interface FourDUanziTableViewController ()
@property(nonatomic,strong)NSMutableArray *modelarray;
@end

@implementation FourDUanziTableViewController

- (void)qingqiu
{
    [Requestmanager requestWithUrl:@"http://ic.snssdk.com/neihan/stream/mix/v1/?content_type=-103&iid=4020526033&os_version=9.2.1&os_api=18&app_name=joke_essay&channel=App%20Store&device_platform=iphone&idfa=D405F887-5B40-46AA-8855-98080F330A6F&vid=17B827F9-5777-4C1B-A49D-3600681A3ED5&openudid=ee80e8b67007cd5f0c2fe22d562c7de66b21d305&device_type=iPhone8,1&version_code=5.0.1&ac=WIFI&screen_width=750&device_id=13430368548&aid=7&content_type=-103&count=100&essence=1&message_cursor=0&min_time=1459904979&mpic=1" requesType:requestTypeGET parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil   ];
        _modelarray = [PictroctModel jiexiModel:dic];
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
        });
    } error:^(NSError *error) {
        NSLog(@"请求错误");
    }];
 
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self qingqiu];
//        [self.tableView registerClass:[FourTableViewCell class] forCellReuseIdentifier:@"four"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _modelarray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellidentify = @"four";
    // 通过indexPath创建cell实例 每一个cell都是单独的
    FourTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    // 判断为空进行初始化  --（当拉动页面显示超过主页面内容的时候就会重用之前的cell，而不会再次初始化）
    if (!cell) {
        cell = [[FourTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellidentify];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    PictroctModel *model = _modelarray[indexPath.row];
    cell.model22 = model;
    [cell fuzhiModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    FourXiangqingViewController *fourxqvc = [[FourXiangqingViewController alloc] init];
    PictroctModel *model = _modelarray[indexPath.row];
    fourxqvc.shareUrl = model.share_url;
    [self.fournav pushViewController:fourxqvc animated:YES];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PictroctModel *model = [[PictroctModel alloc] init];
    model = _modelarray[indexPath.row];
    return [FourTableViewCell cellHeight:model];
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
